package com.xebest.config;

import com.xebest.common.base.controller.RestTemplateAspect;
import com.xebest.common.base.controller.TraceInterceptor;
import com.xebest.common.util.xss.SqlInjectFilter;
import com.xebest.common.util.xss.XssFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 将需要spring管理的javabean在此处配置
 *
 * @author zhouys
 */
@Configuration
public class BeanConfig {

    /**
     * 对RestTemplate增强
     * 使其每次调用二方包时能够将spanID作为参数发送给服务提供者，便于不同系统间进行日志查询
     *
     * @author zhouys
     */
    @Bean
    RestTemplateAspect restTemplateAspect() {
        return new RestTemplateAspect();
    }

    /**
     * 每次请求添加spanID
     * common包中公共日志拦截器
     *
     * @author zhouys
     */
    @Bean
    TraceInterceptor traceInterceptor() {
        return new TraceInterceptor();
    }

    /**
     * 防xss
     *
     * @author 吴飞
     */
    @Bean
    public FilterRegistrationBean xssFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new XssFilter());
        registration.addUrlPatterns("/*");
        return registration;
    }

    /**
     * 防sql注入
     *
     * @author 吴飞
     */
    @Bean
    public FilterRegistrationBean sqlFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new SqlInjectFilter());
        registration.addUrlPatterns("/*");
        return registration;
    }
}
